/**
 * Created by zhangqingxi on 2017/6/6.
 */

var isSearchBarExpand = false;

var index;

var modelPath=ROOT_PATH+'/permission/post/';
var deptModelPath=ROOT_PATH+'/permission/department/';

/**
 * 加载表格
 * @param deptId
 */
function  loadDataList(deptId) {

    $('#dataList').datagrid({
        url:modelPath+'findByParam.shtml',
        queryParams:{
            deptId:deptId,
            sort:'code',
            order:'desc'
        },
        // idField:'id',
        fitColumns:true,
        pagination:true,
        rownumbers:true,
        singleSelect:true,
        sortOrder:true,
        striped:true,
        fit:true,
        columns:[[
            {field:'id',width:10,checkbox:true},
            {field:'code',title:'代码',width:10,sortable:true},
            {field:'name',title:'名称',width:10,sortable:true},
            {field:'useState',title:'启用状态',width:10,sortable:true,formatter:function (value,row,index) {
                if(value===1){
                    return '启用';
                }else {
                    return '停用';
                }
                
            }},
            {field:'addTime',title:'添加时间',width:10,sortable:true},
            {field:'remarks',title:'备注',width:10,sortable:true}
        ]],
        toolbar: '#toolbar'

    });


}

function loadTree() {

    var $departmentTree=$('#departmentTree');

    var selectedTreeNode=$departmentTree.tree('getSelected');

    //初始化组织机构树
    $departmentTree.tree({
        url:deptModelPath+'getDepartmentTree.shtml?parentId=0',
        onSelect:function (node) {

            loadDataList(node.id);
        },
        lines:true,
        onLoadSuccess:function (node, data) {

            var id;
            if(selectedTreeNode!=null){
                id=selectedTreeNode.id;
            }else{
                id=data[0].id
            }
            //找到第一个元素
            selectedTreeNode = $departmentTree.tree('find', id);
            //调用选中事件
            $departmentTree.tree('select', selectedTreeNode.target);
            //打开节点
            $departmentTree.tree('expandTo',selectedTreeNode.target);

            $departmentTree.tree('expand',selectedTreeNode.target);
        }

    });
}


/**
 * 使用layer 打开一个页面
 * @param title
 * @param content
 */
function openLayer(title,content) {

    index=layer.open({
        type: 2,
        title: title,
        shadeClose: true,
        area: ['600px', '380px'],
        content: modelPath+content
    });
}
/**
 * 关闭layer
 */
function closeLayer(data) {
    layer.close(index);
    loadTree();
    layer.msg(data.message,{time:1000,icon:1});
}

$(function () {

    loadTree();

    //查询按钮事件
    $('#btn_search').on('click',function () {
        var $departmentTree=$('#departmentTree');

        //获得选中的树节点
        var selectedTreeNode=$departmentTree.tree('getSelected');

        //获得查询form的参数
        var queryParams=$('#queryForm').serializeJson();

        queryParams["parentId"]=selectedTreeNode.id;

        $('#dataList').datagrid({
            queryParams:queryParams
        });

    });
    //添加按钮事件
    $('#btn_post_add').on('click',function () {
        var $departmentTree=$('#departmentTree');
        var deptId= $departmentTree.tree('getSelected').id;

        var title='添加岗位';
        var content='add.shtml?deptId='+deptId;
        openLayer(title,content);
    });
    //修改按钮事件
    $('#btn_post_edit').on('click',function () {

        var checkedRows=$('#dataList').datagrid('getChecked');
        if(checkedRows.length === 0){
            layer.alert("选择需要修改的行！");
            return
        }
        var id=checkedRows[0].id;
        var title='修改岗位';
        var content='edit.shtml?id='+id;
        openLayer(title,content);
    });
    //删除按钮事件
    $('#btn_post_delete').on('click',function () {

        var checkedRows=$('#dataList').datagrid('getChecked');
        if(checkedRows.length === 0){
            layer.alert("选择需要删除的行！");
            return
        }

        var id=checkedRows[0].id;
        index=layer.confirm('是否确定删除当前选中数据？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            $.ajax({
                type: "POST",
                url: ROOT_PATH+'/permission/post/delete.shtml',
                data: {id:id,delState:1},
                dataType: "json",
                success: function(data){
                    if(data.success){
                        closeLayer(data);
                    }else{
                        layer.msg(data.message,{time:1000,icon:2});
                    }
                }
            });
        });

    })




});